﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

using System.IO;

namespace AL.Security
{
    public sealed class TripleDES
    {



        // Fields
        internal const string IV = "5f34c20f91312878";
        internal const string KEY = "d1b9c9646f2e4ff83a6cce2e5d56dc16b3f7c6ea9fc76393";

        // Methods
        private TripleDES()
        {
        }

        public static string Decrypt(string estr)
        {
            string key = KEY;
            string iv = IV;

            using (
                System.Security.Cryptography.TripleDES edes =
                    System.Security.Cryptography.TripleDESCryptoServiceProvider.Create())
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (
                        System.Security.Cryptography.ICryptoTransform transform = edes.CreateDecryptor(HexToByte(key),
                            HexToByte(iv)))
                    {
                        using (
                            System.Security.Cryptography.CryptoStream stream2 =
                                new System.Security.Cryptography.CryptoStream(stream, transform,
                                    System.Security.Cryptography.CryptoStreamMode.Write))
                        {
                            byte[] buffer = Convert.FromBase64String(estr);
                            stream2.Write(buffer, 0, buffer.Length);
                            stream2.FlushFinalBlock();
                        }
                    }
                    return Encoding.UTF8.GetString(stream.ToArray());
                }
            }
        }

        public static string Encrypt(string str)
        {
            string key = KEY;
            string iv = IV;

            using (
                System.Security.Cryptography.TripleDES edes =
                    System.Security.Cryptography.TripleDESCryptoServiceProvider.Create())
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (
                        System.Security.Cryptography.ICryptoTransform transform = edes.CreateEncryptor(HexToByte(key),
                            HexToByte(iv)))
                    {
                        using (
                            System.Security.Cryptography.CryptoStream stream2 =
                                new System.Security.Cryptography.CryptoStream(stream, transform,
                                    System.Security.Cryptography.CryptoStreamMode.Write))
                        {
                            byte[] bytes = Encoding.UTF8.GetBytes(str);
                            stream2.Write(bytes, 0, bytes.Length);
                            stream2.FlushFinalBlock();
                        }
                    }
                    return Convert.ToBase64String(stream.ToArray());
                }
            }
        }

        public static byte[] HexToByte(string hexString)
        {
            byte[] buffer2 = new byte[((hexString.Length/2) - 1) + 1];
            int num2 = buffer2.Length - 1;
            for (int i = 0; i <= num2; i++)
            {
                buffer2[i] = Convert.ToByte(hexString.Substring(i*2, 2), 0x10);
            }
            return buffer2;
        }


        #region 十六进制转化为字符串

        /// <summary>
        /// 十六进制转化为字符串
        /// </summary>
        /// <param name="ATCom">十六进制字符数组</param>
        /// <returns>字符串</returns>
        public static string HexToString(string str) //例：f9 5b得到 对
        {
            byte[] buffer = getbyteArr(str);
            return Encoding.Unicode.GetString(buffer);
        }

        private static byte[] getbyteArr(string str)
        {
            try
            {
                string[] str1 = str.Split(' ');
                byte[] buffer = new byte[str1.Length];
                for (int i = 0; i < str1.Length; i++)
                {
                    buffer[i] = byte.Parse(str1[i].ToString(), System.Globalization.NumberStyles.HexNumber);
                }
                return buffer;
            }
            catch
            {
                return null;
            }
        }

        #endregion

        #region 字符串转化为十六进制

        /// <summary>
        /// 字符串转化为十六进制
        /// </summary>
        /// <param name="ATCom">字符串</param>
        /// <returns>十六进制字符数组</returns>
        public static string[] StringToHex(string str) //例：对了得到 [f9][5b][86][4e]
        {
            byte[] bytes = Encoding.Unicode.GetBytes(str);
            string[] strArr = new string[bytes.Length];
            for (int i = 0; i < bytes.Length; i++)
            {
                strArr[i] = bytes[i].ToString("x");
            }
            return strArr;
        }

        #endregion






































        // //密钥
        //private const string sKey = "d1b9c9646f2e4ff83a6cce2e5d56dc16b3f7c6ea9fc76393";
        // //向量，必须是12个字符
        //private const string sIV = "5f34c20f91312878";

        // //构造一个对称算法
        // private static SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

        // #region 加密解密函数

        // /// <summary>
        // /// 字符串的加密
        // /// </summary>
        // /// <param name="Value">要加密的字符串</param>
        // /// <returns>加密后的字符串</returns>
        // public static string EncryptString(string Value)
        // {
        //     try
        //     {
        //         ICryptoTransform ct;
        //         MemoryStream ms;
        //         CryptoStream cs;
        //         byte[] byt;
        //         mCSP.Key = Convert.FromBase64String(sKey);
        //         mCSP.IV = Convert.FromBase64String(sIV);
        //         //指定加密的运算模式
        //         mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
        //         //获取或设置加密算法的填充模式
        //         mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
        //         ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);//创建加密对象
        //         byt = Encoding.UTF8.GetBytes(Value);
        //         ms = new MemoryStream();
        //         cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
        //         cs.Write(byt, 0, byt.Length);
        //         cs.FlushFinalBlock();
        //         cs.Close();

        //         return Convert.ToBase64String(ms.ToArray());
        //     }
        //     catch (Exception ex)
        //     {
        //         return ("加密出错： " + ex.Message);
        //     }
        // }

        // /// <summary>
        // /// 解密字符串
        // /// </summary>
        // /// <param name="Value">加密后的字符串</param>
        // /// <returns>解密后的字符串</returns>
        // public static string DecryptString(string Value)
        // {
        //     try
        //     {
        //         ICryptoTransform ct;//加密转换运算
        //         MemoryStream ms;//内存流
        //         CryptoStream cs;//数据流连接到数据加密转换的流
        //         byte[] byt;
        //         //将3DES的密钥转换成byte
        //         mCSP.Key = Convert.FromBase64String(sKey);
        //         //将3DES的向量转换成byte
        //         mCSP.IV = Convert.FromBase64String(sIV);
        //         mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
        //         mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
        //         ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);//创建对称解密对象
        //         byt = Convert.FromBase64String(Value);
        //         ms = new MemoryStream();
        //         cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
        //         cs.Write(byt, 0, byt.Length);
        //         cs.FlushFinalBlock();
        //         cs.Close();

        //         return Encoding.UTF8.GetString(ms.ToArray());
        //     }
        //     catch (Exception ex)
        //     {
        //         return ("解密出错：" + ex.Message);
        //     }
        // }

        // #endregion


    }
}
